﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IPrinting;
using System.Reflection;
using System.IO;
using FastReport;
using FastReport.Data;
using Newtonsoft.Json.Linq;
using FastReport.Barcode;
using ComLib.ComFun;
using FastReport.Export.Pdf;
using LogLib;

namespace FRPrinter
{
    public class FastReportPrinter : IPhotoPrint
    {
        private FRPOption _option = new FRPOption();

        private ConfigDirectory _dirConfig;

        private string _error = "";
        public string ErrorMessage
        {
            get
            {
                return _error;
            }
        }

        public bool Inited
        {
            get;
            private set;
        }

        public bool Exited
        {
            get;
            private set;
        }

        private string _appName = Assembly.GetExecutingAssembly().GetName().Name;
        public string Name
        {
            get
            {
                return _appName;
            }
        }

        public int TimeoutMillisecond
        {
            get;
            set;
        }

        public int ID
        {
            set;
            get;
        }

        public FastReportPrinter()
        {
            _dirConfig = new ConfigDirectory();
            LoadConfig();
        }

        private void LoadConfig()
        {
            if (!File.Exists(_option.ConfigFilePath))
            {
                _option.Save();
            }
            _option.Load();
        }

        public bool Init(string param, out string outParam)
        {
            outParam = "";
            Inited = true;
            return true;
        }

        public void Exit()
        {
            Exited = true;
        }

        public bool Print(string param, out string outParam)
        {
            try
            {
                outParam = "";
                JObject inParams = JObject.Parse(param);
                string photoName = (string)inParams["PhotoName"];
                string photoPath = (string)inParams["PhotoPath"];
                Log.WriteAsync("开始处理打印任务...");
                string pdfFile = string.Format("{0}\\{1}.pdf", _dirConfig.PDFFile, photoName);
                Report report = new Report();
                report.Load(_option.TemplateFilePath);
                Parameter p0 = report.Parameters.FindByName("PhotoPath");
                if (null != p0)
                {
                    p0.Value = photoPath;
                }

                BarcodeObject codeObj1 = report.FindObject("Barcode1") as BarcodeObject;
                if (codeObj1 != null)
                {
                    codeObj1.Text = photoName;
                }
                BarcodeObject codeObj2 = report.FindObject("Barcode2") as BarcodeObject;
                if (codeObj2 != null)
                {
                    codeObj2.Text = photoName;
                }
                report.Prepare();            
                report.PrintSettings.Printer = _option.Printer;
                report.PrintSettings.ShowDialog = false;
                report.Print();

                //PDFExport export = new PDFExport();
                //report.Export(export, pdfFile);
                //JObject outParams = new JObject();
                //outParams.Add("OutputPDFFile", pdfFile);
                //outParam = outParams.ToString();
                Log.WriteAsync("打印任务处理完成.");
                return true;
            }
            catch (Exception ex)
            {
                _error = "打印照片发生了异常:" + ex.Message;
                JObject outParams = new JObject();
                outParams.Add("OutputPDFFile", "");
                outParam = outParams.ToString();
                Log.WriteException(ex.ToString());
                return false;
            }       
        }

        public bool Clear(string param, out string outParam)
        {
            outParam = "";
            PrinterService.ClearPrintQueue(_option.Printer);
            return true;
        }

        public void ShowConfigDialog(string inParam)
        {
            FormConfig form = new FormConfig();
            form.ShowDialog();
        }
    }
}
